package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.AttendanceType;
import com.ruoyi.system.service.IAttendanceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考勤类别Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"考勤类别"})
@RestController
@RequestMapping("/dict/attendance")
public class AttendanceTypeController extends BaseController {
    @Autowired
    private IAttendanceTypeService attendanceTypeService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询考勤类别列表
     */
    @ApiOperation(value = "考勤类别列表", notes = "查询考勤类别列表详情", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceType attendanceType) {
        startPage();
        List<AttendanceType> list = attendanceTypeService.selectAttendanceTypeList(attendanceType);
        return getDataTable(list);
    }

    /**
     * 导出考勤类别列表
     */
    @ApiOperation(value = "考勤类别列表导出", notes = "导出考勤类别列表", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:export')")
    @Log(title = "考勤类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceType attendanceType) {
        List<AttendanceType> list = attendanceTypeService.selectAttendanceTypeList(attendanceType);
        ExcelUtil<AttendanceType> util = new ExcelUtil<AttendanceType>(AttendanceType.class);
        util.exportExcel(response, list, "考勤类别数据");
    }

    /**
     * 获取考勤类别详细信息
     */
    @ApiOperation(value = "获取考勤类别详细信息", notes = "获取考勤类别详细信息详情", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:query')")
    @GetMapping(value = "/{attendanceTypeId}")
    public AjaxResult getInfo(@PathVariable("attendanceTypeId") Long attendanceTypeId) {
        return success(attendanceTypeService.selectAttendanceTypeByAttendanceTypeId(attendanceTypeId));
    }

    /**
     * 新增考勤类别
     */
    @ApiOperation(value = "新增考勤类别", notes = "新增考勤类别详情", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:add')")
    @Log(title = "考勤类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody AttendanceType attendanceType) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        attendanceTypeService.completeData(attendanceType, userId, 1);
        return toAjax(attendanceTypeService.insertAttendanceType(attendanceType));
    }

    /**
     * 修改考勤类别
     */
    @ApiOperation(value = "修改考勤类别", notes = "修改考勤类别详情", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:edit')")
    @Log(title = "考勤类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody AttendanceType attendanceType) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        attendanceTypeService.completeData(attendanceType, userId, 2);
        return toAjax(attendanceTypeService.updateAttendanceType(attendanceType));
    }

    /**
     * 删除考勤类别
     */
    @ApiOperation(value = "删除考勤类别", notes = "删除考勤类别详情", tags = {"考勤类别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:attendance:remove')")
    @Log(title = "考勤类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attendanceTypeIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceTypeIds) {
        return toAjax(attendanceTypeService.deleteAttendanceTypeByAttendanceTypeIds(attendanceTypeIds));
    }
}
